package com.cn.netty.demo1;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.util.CharsetUtil;

/**
 * Created by chenyuncong on 2019/9/2.
 */
public class ServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        /*ByteBuf byteBuf = (ByteBuf)msg;
        System.out.println(byteBuf.toString(CharsetUtil.US_ASCII));
        ctx.writeAndFlush(Unpooled.copiedBuffer(new String("Hello Client").getBytes()));
        ctx.writeAndFlush(Unpooled.copiedBuffer(new String("Hello Client").getBytes()));
        ctx.writeAndFlush(Unpooled.copiedBuffer(new String("Hello Client").getBytes()));*/
        String byteBuf = (String)msg;
        System.out.println(byteBuf);
        ctx.writeAndFlush(Unpooled.copiedBuffer(new String("Hello Client$Hello Client2$").getBytes()));

    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //打印异常信息
        cause.printStackTrace();
        ctx.close();
    }

}
